<?php

/* 
 * Author: Mario Jose
 * email: mariognu-free@yahoo.com.br
 *
 * created: October, 9th 2007
 * modified: July, 25th 2009
*/

/*
  This script is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This script is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Print file csv content in text plain mode, with 2 coluns and
 * print table with table tag.
*/



class printFile
	{
   /*
    * vars with content file and errors control
    *
    * @var bool $flag
    * @var array $content Have content of file csv open
    * @var string @errorMsg Default message error
   */
	var $flag;
	var $content;
	var $errorMsg = "N&atilde;o dispon&iacute;vel";

   /*
    * Open csv file and make var $content with file content
    *
    * @param string $fileName Have a file neme that will be read
    * @param string $split Have character that split witch element
   */	
	function printFile($fileName, $split)
		{
		if (!file_exists($fileName)) $this->flag = FALSE;
		else
			{
			$this->flag = TRUE;
			$handler = fopen ($fileName,"r");
         
         $i = 0;
			while ($rows = fgetcsv($handler, 2048, $split))
				{
				
            // make array of elements				
				for ($j = 0; $j < (count($rows)); $j++)
					{
					$this->content[$i][$j] = $this->handleText($rows[$j]);
					}
            
            $i++;
				}
         fclose ($handler);
			}
		}// end printFile

      /*
       * Print file content in text plain. Is used to print file that
       * have 2 coluns like comunications or news files
      */
		function printTextPlain()
			{
         // $i is 1 to don't print head
			for ($i = 1; $i < count($this->content); $i++)
				{ 
            echo '    <p>' . "\r";
	   		echo '      <span><strong><em>' . $this->content[$i][0] . '</em></strong></span><br />' . "\r";
	   		echo '      <span>' . $this->content[$i][1] . '</span>' . "\r";
				echo '    </p>' . "\r";
				}
			}// end function

      /*
        * Print file content in table format with table tags
        *
        * @param string $tableName Have a class name used in table
        * @param bool $autoNum Show or not line number
      */
		function printContent($tableName = NULL, $autoNum = FALSE)
			{
			if ($tableName != NULL) echo '   <table id="' . $tableName . '" width="97%" border=0>' . "\r";
			else echo '   <table width="97%" border=0>' . "\r";
			
			for ($i = 0; $i < count($this->content); $i++)
				{
				echo "      <tr>" . "\r";
				for ($j = 0; $j < count($this->content[0]); $j++)
					{
					if ($i == 0) // first line have col names
						{
						echo "        <td><strong>" . $this->content[$i][$j] . "</strong></td>" . "\r";
						}
					else
						{
						if ($autoNum === TRUE ) echo "        <td>" . $i . ". " . $this->content[$i][$j] . "</td>" . "\r";
						else echo "        <td>" . $this->content[$i][$j] . "</td>" . "\r";
						}
					}
				echo "      </tr>" . "\r";
				}
			echo "   </table>" . "\r";
		}// end function

      /*
        * Clean file content fo special character and trade 
        * this to html mark
        *
        * @param string $text Have text that cleaned
      */		
		function handleText($text)
			{
				// remove special end chars
				$cleanText = str_replace("\n", "", str_replace("\r", "", $text));
				// translate text to html format
				$htmlText = htmlentities($cleanText);
				return $htmlText;
			} //end function
			
	} //end class

?>